package com.tywj.panda.travel

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.tywj.lib.core.common.extension.onClick
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.widget.recycleview.adapter.BaseViewHolder
import com.tywj.panda.customer.common.constant.TravelStatusConstant
import com.tywj.panda.customer.common.entity.TravelEntity
import com.tywj.panda.customer.common.widget.recyclerview.MyBaseAdapter
import com.tywj.panda.travel.route.TravelRoute

/**
 *行程详情适配器
 */
class TravelAdapter : MyBaseAdapter<TravelEntity.DataBean>(R.layout.travel_recycle_item_travel) {

    override fun convert(helper: BaseViewHolder, item: TravelEntity.DataBean) {
        super.convert(helper, item)
        helper.setText(R.id.tvLineName, item.lineName)
            .setText(R.id.tvStart, item.startStation)
            .setText(R.id.tvEnd, item.endStation)
            .setText(R.id.tvStartTime, item.onTime)
            .setText(R.id.tvEndTime, item.offTime)
            .setText(R.id.tvNum, item.number.toString() + "人")
            .setText(R.id.tvCity, item.cityName)
            .setText(R.id.tvStatus, getStatus(item.status))

        helper.itemView.onClick {
            TravelRoute.toTravelDetails(item).navigation(mContext)
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {

                val position = parent.getChildLayoutPosition(view)
                if (position != RecyclerView.NO_POSITION) {
                    when (position) {
                        // 第一个
                        0 -> {
                            outRect.bottom = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_10)
                        }
                        // footer
                        this@TravelAdapter.data.size + this@TravelAdapter.headerLayoutCount -> outRect.top = 0
                        // 其他
                        else -> outRect.bottom = ResourcesUtil.getDimensionPixelSize(com.tywj.navigation.R.dimen.dp_10)
                    }
                }
            }
        })
    }

    private fun getStatus(status: Int): String {

        return when (status) {
            TravelStatusConstant.STATUS_NO_TICKET -> {
                "未出票"
            }
            TravelStatusConstant.STATUS_WAIT_CAR -> {
                "待派车"
            }
            TravelStatusConstant.STATUS_HAD_CAR -> {
                "已派车"
            }
            TravelStatusConstant.STATUS_TYPE_HAD_RECOUNT -> {
                "已验票"
            }
            TravelStatusConstant.STATUS_TYPE_HAD_EXPIRED -> {
                "已过期"
            }
            TravelStatusConstant.STATUS_TYPE_REFUND_ACCEPTED -> {
                "退票已受理"
            }
            TravelStatusConstant.STATUS_TYPE_HAD_REFUND -> {
                "已退票"
            }
            else -> ""
        }
    }
}